#include "StdAfx.h"
#include "AccessNet.h"
#include <WinInet.h>

#include "active-newDlg.h"

CString getFtpServeUrl = _T("http://61.183.149.138/GetFTPUrl.jspx?target=");
CString updateServerUrl = _T("http://61.183.149.138/SetTestData.jspx?action=updn&");
CString imptHost=_T("61.183.149.138");//to change port ::KEY = INTERNET_DEFAULT_HTTP_PORT
CString imptPage=_T("SetTestData.jspx?action=icmp");
//debug
//CString getFtpServeUrl = _T("http://172.16.3.54/netest/GetFTPUrl.jspx?target=");
//CString updateServerUrl = _T("http://172.16.3.54/netest/SetTestData.jspx?action=updn&");
//CString imptHost=_T("172.16.3.54");
//CString imptPage=_T("netest/SetTestData.jspx?action=icmp");

AccessNet::AccessNet(void)
{
}

AccessNet::~AccessNet(void)
{
}

bool AccessNet::UpLoadFtpFile(TFtp &aServer, CString aUrl, CString aFile, TResult &aRst, CactivenewDlg *m_pDlg)
{
	if (!CheckFtpServer(aServer,m_pDlg->m_Local_Ip,m_pDlg->m_Agent))
	{
		return false;
	}
	CString sURL,sFile;
	sURL = aUrl;
	sFile = aFile;
	BOOL bRet = 0;
	DWORD dwRead = 0;
	DWORD dwWrite = 0;
	HANDLE hFile = NULL;
	HINTERNET hInternetOpen = NULL;
	HINTERNET hInternetConnect = NULL;
	HINTERNET hInternetOpenUrl = NULL;
	BYTE *pBuf = new BYTE[80*1024];
	DWORD total = 0;
	ZeroMemory(pBuf,80*1024);
#ifndef _DEBUG
	memset(pBuf, 1, 80*1024);
#endif
	clock_t start = clock();
	m_pDlg->atmpResult.m_size = 0;
	m_pDlg->atmpResult.m_time = clock() -start;
	m_pDlg->atmpResult.m_total = 5*1024*1024;
	::PostMessage(m_pDlg->m_hWnd, 0x403, 1, NULL);
	{
		hInternetOpen=InternetOpen(_T("UploadFtpFile"),INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
		if (hInternetOpen==NULL)
		{
			delete []pBuf;
			return false;
		}
		hInternetConnect = InternetConnect(hInternetOpen, aServer.m_Server, 21, aServer.m_Name,
			aServer.m_Pass, INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
		if (hInternetConnect == NULL)
		{
			InternetCloseHandle(hInternetOpen);
			delete []pBuf;
			return false;
		}
		hInternetOpenUrl = FtpOpenFile(hInternetConnect, sURL, GENERIC_WRITE, FTP_TRANSFER_TYPE_BINARY,0);
		if (hInternetOpenUrl==NULL)
		{
			InternetCloseHandle(hInternetConnect);
			InternetCloseHandle(hInternetOpen);
			delete []pBuf;
			return false;
		}
#ifdef _DEBUG
		hFile=CreateFile(sFile,GENERIC_READ,0,0,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
		if (hFile==INVALID_HANDLE_VALUE)
		{
			InternetCloseHandle(hInternetOpenUrl);
			bool df = FtpDeleteFile(hInternetConnect, sURL);
			InternetCloseHandle(hInternetConnect);
			InternetCloseHandle(hInternetOpen);
			delete []pBuf;
			return false;
		}
#endif
		while(1)
		{
			m_pDlg->atmpResult.m_size = total;
			m_pDlg->atmpResult.m_time = clock() -start;
			::PostMessage(m_pDlg->m_hWnd, 0x403, 1, NULL);
#ifdef _DEBUG
			bRet = ReadFile(hFile, pBuf, 80*1024, &dwRead, NULL);
			if(!bRet)
			{
				break;
			}
			if(!dwRead)
			{
				break;
			}
#else
			dwRead = 80*1024;
			if (total >= 80*1024*64)
			{
				break;
			}
#endif
			
			total += dwRead;
			bRet = InternetWriteFile(hInternetOpenUrl, pBuf, dwRead, &dwWrite);
			if (bRet==0)
			{
				CloseHandle(hFile);
				InternetCloseHandle(hInternetOpenUrl);
				bool df = FtpDeleteFile(hInternetConnect, sURL);
				InternetCloseHandle(hInternetConnect);
				InternetCloseHandle(hInternetOpen);
				delete []pBuf;
				return false;
			}
		}
		int dtime = clock()-start;
		aRst.m_size = total;
		aRst.m_time = dtime;
		CloseHandle(hFile);
		InternetCloseHandle(hInternetOpenUrl);
		bool df = FtpDeleteFile(hInternetConnect, sURL);
		InternetCloseHandle(hInternetConnect);
		InternetCloseHandle(hInternetOpen);
		delete []pBuf;
		return true;
	}
}

bool AccessNet::DownLoadFtpFile(TFtp &aServer, CString aUrl, CString aFile, TResult &aRst, CactivenewDlg *m_pDlg)
{
	if (!CheckFtpServer(aServer,m_pDlg->m_Local_Ip,m_pDlg->m_Agent))
	{
		return false;
	}
	CString sURL,sFile;
	sURL = aUrl;
	sFile = aFile;
	BOOL bRet = 0;
	DWORD dwRead = 0;
	DWORD dwWrite = 0;;
	HANDLE hFile = NULL;
	HINTERNET hInternetOpen = NULL;
	HINTERNET hInternetConnect = NULL;
	HINTERNET hInternetOpenUrl = NULL;
	BYTE *pBuf = new BYTE[100*1024];
	DWORD total = 0;
	ZeroMemory(pBuf,100*1024);
	clock_t start = clock();
	m_pDlg->atmpResult.m_size = 0;
	m_pDlg->atmpResult.m_time = clock() -start;
	m_pDlg->atmpResult.m_total = 5*1024*1024;
	::PostMessage(m_pDlg->m_hWnd, 0x403, 0, NULL);
	{
		hInternetOpen=InternetOpen(_T("DownloadFtpFile"),INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
		if (hInternetOpen==NULL)
		{
			delete []pBuf;
			return false;
		}
		hInternetConnect = InternetConnect(hInternetOpen, aServer.m_Server, 21, aServer.m_Name,
			aServer.m_Pass, INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
		if (hInternetConnect == NULL)
		{
			InternetCloseHandle(hInternetOpen);
			delete []pBuf;
			return false;
		}
		hInternetOpenUrl = FtpOpenFile(hInternetConnect, sURL, GENERIC_READ, FTP_TRANSFER_TYPE_BINARY,0);
		if (hInternetOpenUrl==NULL)
		{
			InternetCloseHandle(hInternetConnect);
			InternetCloseHandle(hInternetOpen);
			delete []pBuf;
			return false;
		}
#ifdef _DEBUG
		hFile=CreateFile(sFile,GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
		if (hFile==INVALID_HANDLE_VALUE)
		{
			InternetCloseHandle(hInternetOpenUrl);
			InternetCloseHandle(hInternetConnect);
			InternetCloseHandle(hInternetOpen);
			delete []pBuf;
			return false;
		}
#endif
		int aHighlen, aLowlen;
		aLowlen = FtpGetFileSize(hInternetOpenUrl, (LPDWORD)&aHighlen);
		while(1)
		{
			bRet=InternetReadFile(hInternetOpenUrl,pBuf,100*1024,&dwRead);
			if(!bRet)
			{
				break;
			}
			if(!dwRead)
			{
				break;
			}
			total += dwRead;
			m_pDlg->atmpResult.m_size = total;
			m_pDlg->atmpResult.m_time = clock() -start;
			m_pDlg->atmpResult.m_total = aLowlen;
			::PostMessage(m_pDlg->m_hWnd, 0x403, 0, NULL);
#ifdef _DEBUG
			bRet = WriteFile(hFile,pBuf,dwRead,&dwWrite,NULL);
#else
			bRet = 1;
#endif
			if (bRet==0)
			{
				CloseHandle(hFile);
				InternetCloseHandle(hInternetOpenUrl);
				InternetCloseHandle(hInternetConnect);
				InternetCloseHandle(hInternetOpen);
				delete []pBuf;
				return false;
			}
		}
		int dtime = clock()-start;
		aRst.m_size = total;
		aRst.m_time = dtime;
		CloseHandle(hFile);
		InternetCloseHandle(hInternetOpenUrl);
		InternetCloseHandle(hInternetConnect);
		InternetCloseHandle(hInternetOpen);
		delete []pBuf;
		return true;
	}
}

bool AccessNet::DoHttpPost(CString aActionData, CString &aRst)
{
	CString sURL,sData,sPage;
	sURL = imptHost;
	sPage = imptPage;
	sData = aActionData;
	BOOL bRet = 0;
	DWORD dwRead = 0;
	DWORD dwWrite = 0;;
	HANDLE hFile = NULL;
	HINTERNET hInternetOpen = NULL;
	HINTERNET hInternetConnect = NULL;
	HINTERNET hInternetOpenUrl = NULL;
	BYTE *pBuf = new BYTE[100*1024];
	DWORD total = 0;
	ZeroMemory(pBuf,100*1024);
	clock_t start = clock();
	{
		hInternetOpen=InternetOpen(_T("Http Post"),INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
		if (hInternetOpen==NULL)
		{
			delete []pBuf;
			return false;
		}
		//TODO replace to port of web.
		hInternetConnect = InternetConnect(hInternetOpen, sURL, /*8080*/INTERNET_DEFAULT_HTTP_PORT, NULL,
			NULL, INTERNET_SERVICE_HTTP, 0, 0);
		if (hInternetConnect == NULL)
		{
			InternetCloseHandle(hInternetOpen);
			delete []pBuf;
			return false;
		}
		hInternetOpenUrl = HttpOpenRequest(hInternetConnect, _T("POST"), sPage, NULL, NULL, NULL, 0, 1);
		if (hInternetOpenUrl==NULL)
		{
			InternetCloseHandle(hInternetConnect);
			InternetCloseHandle(hInternetOpen);
			delete []pBuf;
			return false;
		}

		CString aHeader = _T("Content-Type: application/x-www-form-urlencoded");
		BOOL rtAddHeader = HttpAddRequestHeaders(hInternetOpenUrl, aHeader, aHeader.GetLength(), HTTP_ADDREQ_FLAG_ADD_IF_NEW);

		char* aSData = (char*)(LPCTSTR)sData;

		BOOL req = HttpSendRequest(hInternetOpenUrl, NULL, 0, aSData, strlen(aSData));
		if (!req)
		{
			InternetCloseHandle(hInternetOpenUrl);
			InternetCloseHandle(hInternetConnect);
			InternetCloseHandle(hInternetOpen);
			delete []pBuf;
			return false;
		}
			TCHAR szBuffer[80];
			DWORD dwLen = _countof(szBuffer), dwStatusCode = 0, dwContenLen = 0;
			BOOL blQuery = HttpQueryInfo(hInternetOpenUrl, HTTP_QUERY_STATUS_CODE, szBuffer, &dwLen, NULL);
			if (blQuery)
				dwStatusCode = (DWORD) _ttol(szBuffer);
			if (dwStatusCode == 200)
			{
				while(1)
				{
					bRet=InternetReadFile(hInternetOpenUrl,pBuf,100*1024,&dwRead);
					if(!bRet)
					{
						break;
					}
					if(!dwRead)
					{
						break;
					}
					total += dwRead;
					bRet=1;//WriteFile(hFile,pBuf,dwRead,&dwWrite,NULL);
					aRst.Append((char *)pBuf);
					if (bRet==0)
					{
						CloseHandle(hFile);
						InternetCloseHandle(hInternetOpenUrl);
						InternetCloseHandle(hInternetConnect);
						InternetCloseHandle(hInternetOpen);
						delete []pBuf;
						return false;
					}
				}
			}
			else
			{
				InternetCloseHandle(hInternetOpenUrl);
				InternetCloseHandle(hInternetConnect);
				InternetCloseHandle(hInternetOpen);
				delete []pBuf;
				return false;
			}
		int dtime = clock()-start;
		CloseHandle(hFile);
		InternetCloseHandle(hInternetOpenUrl);
		InternetCloseHandle(hInternetConnect);
		InternetCloseHandle(hInternetOpen);
		delete []pBuf;
		return true;
	}
}

bool AccessNet::DoHttpGet(CString aUrl, CString aFile, CString &aRst,CString &aAgent)
{
	CString sURL,sFile;
	sURL = aUrl;
	sFile = aFile;
	BOOL bRet = 0;
	DWORD dwRead = 0;
	DWORD dwWrite = 0;;
	HANDLE hFile = NULL;
	HINTERNET hInternetOpen = NULL;
	HINTERNET hInternetOpenUrl = NULL;
	BYTE *pBuf = new BYTE[100*1024];
	ZeroMemory(pBuf,100*1024);
	{
		hInternetOpen=InternetOpen(aAgent,INTERNET_OPEN_TYPE_PRECONFIG,NULL,NULL,0);
		if (hInternetOpen==NULL)
		{
			delete []pBuf;
			return false;
		}
		hInternetOpenUrl=InternetOpenUrl(hInternetOpen,sURL,NULL,0,INTERNET_FLAG_RELOAD,0);
		if (hInternetOpenUrl==NULL)
		{
			InternetCloseHandle(hInternetOpen);
			delete []pBuf;
			return false;
		}
#ifdef _DEBUG
		hFile=CreateFile(sFile,GENERIC_WRITE,0,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);
		if (hFile==INVALID_HANDLE_VALUE)
		{
			InternetCloseHandle(hInternetOpenUrl);
			InternetCloseHandle(hInternetOpen);
			delete []pBuf;
			return false;
		}
#endif
		while(1)
		{
			bRet=InternetReadFile(hInternetOpenUrl,pBuf,100*1024,&dwRead);
			if(!bRet)
			{
				break;
			}
			if(!dwRead)
			{
				break;
			}
			aRst.Append((char *)pBuf);
#ifdef _DEBUG
			bRet = WriteFile(hFile,pBuf,dwRead,&dwWrite,NULL);
#else
			bRet = 1;
#endif
			if (bRet==0)
			{
				CloseHandle(hFile);
				InternetCloseHandle(hInternetOpenUrl);
				InternetCloseHandle(hInternetOpen);
				delete []pBuf;
				return false;
			}
		}
		CloseHandle(hFile);
		InternetCloseHandle(hInternetOpenUrl);
		InternetCloseHandle(hInternetOpen);
		delete []pBuf;
		return true;
	}
}


bool AccessNet::CheckFtpServer(TFtp &aFtp,CString &aLocalIp,CString &aAgent)
{
	bool rnt = true;
	//if (aFtp.m_Name.IsEmpty())
	{
		CString  sFile, aRst;
		CString aUrl;
		aUrl.Append(getFtpServeUrl);
		aUrl.Append(aLocalIp);
		sFile = _T("c:\\ftp.txt");
		rnt = DoHttpGet(aUrl, sFile, aRst,aAgent);
		if (rnt)
		{
			rnt = DoParseFtpUrl(aFtp, aRst.Trim());
		}
		else
			return rnt;
	}
	return rnt;
}
/*
	aUrl="ftp://test:test@172.16.2.39/"
*/
bool AccessNet::DoParseFtpUrl(TFtp &aFtp, CString aUrl)
{
	bool rnt = false;
	CString m_Url = aUrl;
	int pos1 = m_Url.Find("//");
	if (pos1 > 0)
		m_Url = m_Url.Mid(pos1 + 2);
	else
		return rnt;
	int pos2 = m_Url.Find(":");
	int pos3 = m_Url.Find("@");
	int pos4 = m_Url.Find("/");
	if (pos2 > 0 && pos3 > pos2)
	{
		rnt = true;
		aFtp.m_Name = m_Url.Mid(0, pos2);
		aFtp.m_Pass = m_Url.Mid(pos2 + 1, pos3 - pos2 - 1);
		if (pos4 > pos3)
			aFtp.m_Server = m_Url.Mid(pos3 + 1, pos4 - pos3 - 1);
		else
			aFtp.m_Server = m_Url.Mid(pos3 + 1);
	}
	return rnt;
}

bool AccessNet::UpDateRst(CString &aRst, TResult aDnRst,TResult aUpRst)
{
	CString aUrl;
	aUrl.Append(updateServerUrl);
	//dsize=1024000&dtime=30 http://61.183.149.138/netest/SetTestData.jspx?action=updn&upsize=1024&uptime=10&dnsize=2055&dntime=3
	aUrl.AppendFormat("upsize=%d&uptime=%d&dnsize=%d&dntime=%d", aUpRst.m_size, aUpRst.m_time, aDnRst.m_size, aDnRst.m_time);
	CString aFile = _T("c:\\update.txt");
	CString aAgent = _T("...");
	return DoHttpGet(aUrl, aFile, aRst,aAgent);
}
